/*
 * FileManage.h
 *
 *  Created on: Mar 27, 2013
 *      Author: root
 */

#ifndef FILEMANAGE_H_
#define FILEMANAGE_H_

#include "SvrConfig.h"
#include "Svr_log.h"
#include "link_list.h"

#define INVALID_HANDLE_VALUE -1

/*
 *Server virtual path map
 */
const size_t VIR_DIR_MAP_NUM = 8;
struct Virtual_Map
{
     char vir_dir[FILE_MAX_PATH];
     char disc_dir[FILE_MAX_PATH];
};

/*
*File Info Data
*/
struct FileInfoData
{
	char file_name[FILE_MAX_PATH];
	int  count;
	int  del_flag;
	time_t begin_time;

	FileInfoData * next;
	FileInfoData * prev;

	FileInfoData()
	{
		memset(file_name,'\0',sizeof(file_name));
		count = 0;
		del_flag = 0;
		begin_time = time(NULL);
	}
};

/*
 * SvrFileManage class
 */
class SvrFileManage {
public:
	SvrFileManage();
	virtual ~SvrFileManage();

	int init(SvrConfig * pconfig, Svr_log * psvrlog);

	/*
	*get vir directory
	*/
	bool  get_vir_dir(char * virname,char * buf, size_t buf_len);

	/**
	 * add delete file
	 */
	int add_deletefile(const char * fileName,int count);

	/**
	 * open file by filename
	 * filesize:set file size
	 * is_create_append: open file by create or append
	 * return : filefd, error -1
	 */
	int open_file(const char * filename,uint64_t & filesize,bool is_create_append = false);

	/*
	 * SvrFileManage tick run
	 */
	void run(long tickvalue);

protected:
	/*
	*set vir directory list
	*/
	bool  set_vir_dir(const char * virliststr);

	/*
	* delete file
	*/
	bool delete_file(const char * filename);

	/**
	 * delete file ist
	 */
	int delete_file_list(void);

	/*
	 * create dir by file full path
	 * */
	bool create_dir(const char * filename);

private:
	/*
	*Virtual map list ,by config args
	*/
	Virtual_Map m_virdirmap[VIR_DIR_MAP_NUM];
	int m_virtual_num;

	/*
	*del content list
	*/
	link_list<FileInfoData> del_content_list;
	smart_lock del_content_list_lock;

	/*
	*svr config
	*/
	SvrConfig  * pSvrconfig;
	Svr_log    * pSvrlog;

	/*time tick*/
	int	FileClearTick;
};

#endif /* FILEMANAGE_H_ */
